﻿using System;
using System.IO;
using System.Diagnostics;
using System.Xml.XPath;
using System.Collections.Generic;
using System.Management.Instrumentation;

namespace Dynamic.Facades.EntLib.Logging
{
    /// <summary>
    /// XML日志实体
    /// </summary>
    [Serializable]
    public class XmlLogEntry : LogEntry
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public XmlLogEntry() : base() { }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="priority"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="title"></param>
        /// <param name="properties"></param>
        public XmlLogEntry(object message, EventCategory category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
            : base(message, category, priority, eventId, severity, title, properties) { } 

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="message"></param>
        /// <param name="category"></param>
        /// <param name="priority"></param>
        /// <param name="eventId"></param>
        /// <param name="severity"></param>
        /// <param name="title"></param>
        /// <param name="properties"></param>
        protected XmlLogEntry(object message, ICollection<string> category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties) 
            : base(message, category, priority, eventId, severity, title, properties) { }


        private string xmlString = null;

        [NonSerialized]
        private XPathNavigator xml;

        /// <summary>
        /// XML
        /// </summary>
        [IgnoreMember]
        public XPathNavigator Xml
        {
            get
            {
                if (xml == null && !string.IsNullOrEmpty(xmlString))
                {
                    TextReader reader = new StringReader(xmlString);
                    xml = new XPathDocument(reader).CreateNavigator();
                }
                return xml;
            }
            set
            {
                if (xmlString == null && value != null)
                {
                    xmlString = value.InnerXml;
                }
                else
                {
                    xmlString = null;
                }
                xml = value;
            }
        }
    }
}
